package com.aeye.mbr.upms.rpc.service.impl;


import com.aeye.mbr.common.plugin.mapper.BaseServiceImpl;
import com.aeye.mbr.common.result.BootstrapTableParams;
import com.aeye.mbr.common.result.BootstrapTableResult;
import com.aeye.mbr.upms.dao.model.UpmsDict;
import com.aeye.mbr.upms.rpc.api.UpmsDictService;
import com.aeye.mbr.upms.rpc.mapper.UpmsDictMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * <p>Description: </p>
 *
 * @author zhouchuanbo
 * @version 1.0
 * @date 2018/3/19 0019
 */
public class UpmsDictServiceImpl extends BaseServiceImpl<UpmsDict> implements UpmsDictService {

    private static Logger _log = LoggerFactory.getLogger(UpmsDictServiceImpl.class);

    @Autowired
    UpmsDictMapper dictMapper;

    /**
     * Description: 查询字典列表
     *
     * @param params
     * @param fieldName
     * @param fieldComment
     * @author zhouchuanbo
     * @date 下午 2:38 2018/3/19 0019
     */
    @Override
    public BootstrapTableResult<UpmsDict> selectDictListByParams(BootstrapTableParams params, String fieldName, String fieldComment) {
        BootstrapTableResult result = new BootstrapTableResult();
        if (params != null) {
            PageHelper.startPage(params.getPage(), params.getLimit());
            Example example = new Example(UpmsDict.class);
            String sort = params.getSort();
            String order = params.getOrder();
            String search = params.getSearch();
            if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
                example.setOrderByClause(sort + " " + order);
            }
            if (StringUtils.isNotBlank(fieldName)) {
                example.or()
                        .andLike("fieldName", "%" + fieldName + "%");
            }
            if (StringUtils.isNotBlank(fieldComment)) {
                example.or()
                        .andLike("fieldComment", "%" + fieldComment + "%");
            }
            List<UpmsDict> dicts = dictMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(dicts)) {
                if (dicts instanceof Page) {
                    Page<UpmsDict> pls = (Page) dicts;
                    result.setTotal((int) pls.getTotal());
                    result.setRows(pls.getResult());
                } else {
                    result.setTotal(dicts.size());
                    result.setRows(dicts);
                }
            }else
            {
                result.setTotal(dicts.size());
                result.setRows(dicts);
            }
        }
        return result;
    }

    /**
     * Description:获取字典列表
     *
     * @author zhouchuanbo
     * @date 上午 11:34 2018/3/23 0023
     */
    @Override
    public List<UpmsDict> getAllDicts() {
        return dictMapper.selectAll();
    }

    @Override
    public List<UpmsDict> selectDictList(UpmsDict dict) {
        Example example = new Example(UpmsDict.class);
        example.or().andEqualTo("appFieldName",dict.getAppFieldName());
        return dictMapper.selectByExample(example);
    }
}

